package DataBase;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Vector;

public class GetDecisionAboutButtonEnterOrExit {
	
	private String empID;
	private String date;
	private int flag;                                  //if flag=1 open button ENTER,if flag=0 open button EXIT
	
	public	GetDecisionAboutButtonEnterOrExit(String emp,String dateToday)
	{
		this.empID=emp;
		this.date=dateToday;
	}

	
	
	
	public void CheckAndSave() 
	{
		SQLConnection sc=new SQLConnection();
		//--------------------------------------------
		 
		try {
			  
			PreparedStatement ps3 = sc.getConnection().prepareStatement("SELECT max(work_hrs_id) FROM employee_working_hours WHERE Eid=?");   //("SELECT work_hrs_id FROM employee_working_hours WHERE Eid=? and date=? and clock_out=null");        //("SELECT MAX(work_hrs_id) MAXid FROM employee_working_hours WHERE Eid=? and date=?;");
				//	sc.getConnection().setAutoCommit(false); // ubrat
					ps3.setString(1, empID);
				 
					ResultSet rsps=ps3.executeQuery(); 
					int work_hrs_idInt=0;                   
		   
					while (rsps.next())
					{
					
						work_hrs_idInt=rsps.getInt(1);

						if(work_hrs_idInt==0){  work_hrs_idInt=0;		}	 
							
						
					}
					 
		 
		//try {
			PreparedStatement ps1 = sc.getConnection().prepareStatement("SELECT date_out IS NULL   FROM employee_working_hours WHERE Eid=? and work_hrs_id=?;");   //problema esli data nachala i konza raznie -  and date=? ;");
			ps1.setString(1, empID);
			ps1.setInt(2, work_hrs_idInt);
			ResultSet rs= ps1.executeQuery();	
			
			if(rs.next())
			{
				
				rs.previous();
			    while(rs.next())
			    {
			    
				if( rs.getBoolean(1))   { flag=0; }              //true(clock_out is null) we need button EXIT
				 
				else {  flag=1; }                               //false(clock_out is not null) we need button ENTER
				 
			    }
			}
				
			
			else  {  flag=1;   }   //open button Enter 
				 
			
			sc.getConnection().close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}




	public int getFlag() {
		return flag;
	}




	public void setFlag(int flag) {
		this.flag = flag;
	}

	
}
